/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef GEOFENCE_TRACK_H
#define GEOFENCE_TRACK_H

#include <string>
#include <memory>
#include <vector>
#include <thread>
#include <map>
#include <errors.h>
#include "datetime_ex.h"
#include "rdb_store.h"
#include "rdb_helper.h"
#include "rdb_open_callback.h"
#include "rdb_store_config.h"
#include "values_bucket.h"
#include "result_set.h"
#include "geofence_common.h"
#include "geofence_data_utils.h"
#include "geofence_impl_client.h"
#include "geofence_config.h"

#include "power_mgr_client.h"
#include "power_state_machine_info.h"

namespace OHOS {
namespace Msdp {

class GeofenceService;
class GeofenceTrack {
public:
    explicit GeofenceTrack(const wptr<GeofenceService>& ms) : ms_(ms)
    {
        GEOFENCE_HILOGI(GEOFENCE_MODULE_SERVICE, "GeofenceTrack instance is created.");
    }
    ~GeofenceTrack() = default;

    class GeofenceCallbackDeathRecipient : public IRemoteObject::DeathRecipient {
    public:
        GeofenceCallbackDeathRecipient() = default;
        virtual void OnRemoteDied(const wptr<IRemoteObject> &remote);
        virtual ~GeofenceCallbackDeathRecipient() = default;
    };

    enum EventType {
           EVENT_UEVENT_FD,
           EVENT_TIMER_FD,
    };

    bool Init();
    void InitTimer();
    void SetTimerInterval(int interval);
    void TimerCallback();
    int RegisterTimerCallback(const int fd, const EventType et);
    void StartThread();
    void LoopingThreadEntry();
    void LocationCellData(std::string cell);
    std::string GetTimeNowString();
    void SetTrack();

private:
    const wptr<GeofenceService> ms_;
    sptr<IRemoteObject::DeathRecipient> geofenceCBDeathRecipient_;

    using Callback = std::function<void(GeofenceTrack*)>;
    std::map<int32_t, Callback> callbacks_;
    int timerInterval_ = -1;
    int32_t timerFd_ = -1;
    int32_t epFd_ = -1;

    std::unique_ptr<GeofenceImplClient> geofenceImplClient_;
    std::shared_ptr<GeofenceConfig> geofenceConfig_ = std::make_shared<GeofenceConfig>();
    int32_t timeCache_ = 0;
    int32_t trackTestFlag_;

};
} // namespace Msdp
} // namespace OHOS
#endif // GEOFENCE_TRACK_H

